package 接口.Clonable接口和深拷贝;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-06-18
 * Time: 22:46
 */
/*
Clonable 接口和深拷贝
Java 中内置了一些很有用的接口, Clonable 就是其中之一.
Object 类中存在一个 clone 方法, 调用这个方法可以创建一个对象的 "拷贝". 但是要想合法调用 clone 方法, 必须要
先实现 Clonable 接口, 否则就会抛出 CloneNotSupportedException 异常
 */

class Animal1 implements Cloneable {
    private String name;

    @Override
    /*protected Object clone() throws CloneNotSupportedException*/
    public Animal1 clone() {
        Animal1 o = null;
        try {
            o = (Animal1)super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }
}
public class Test {
    public static void main(String[] args) {
        Animal1 animal1 = new Animal1();
        Animal1 animal2 = animal1.clone();
        System.out.println(animal1 == animal2);
    }
}
